export function isValidKey (key: string | number | symbol, object: object): key is keyof typeof object {
  return key in object
}

/**
 * 使用json进行深克隆
 */
export const deepCloneByJson = (obj: object) => JSON.parse(JSON.stringify(obj))

/**
 * 随机获取[n-m]之间的随机整数
 */
export const getRandomInt = (min: number, max: number) => {
  return Math.floor(Math.random() * (max - min + 1)) + min
}
export const randomString = (length: number) => {
  const str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let res = ''
  for (let i = 0; i < length; i += 1) {
    res += str.charAt(getRandomInt(0, str.length - 1))
  }
  return res
}

const stylePools = []
const styleIndex = 0

export const generaterStyle = (data: any) => {
  const styleEle = document.createElement('style')
  const id = `generaterStyle-${styleIndex + 1}`
  let styleContent = ':root {'
  Object.keys(data).forEach((key: any) => {
    const styleVal = data[key]
    styleContent += `${key}:${styleVal};`
  })
  styleContent += '}'
  styleEle.textContent = styleContent
  styleEle.id = id
  stylePools.push(id)
  document.head.appendChild(styleEle)
}

export const inBrowser = typeof window !== 'undefined'

export const ua = () => {
  const userAgentInfo = inBrowser ? navigator.userAgent : ''
  const Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPod']
  let flag = 'PC'
  for (let vv = 0; vv < Agents.length; vv++) {
    if (userAgentInfo.indexOf(Agents[vv]) > 0) {
      flag = Agents[vv]
      break
    }
  }
  return flag
}
